home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1 / Ian and Stuart's One (Australia).iso / Australasian Legends / Commercial / Rainbow Hill / MacDOS™ 2.0.0 / serial comms / SendToPort.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  4KB  |  147 lines

  1. // sendToPort.c
  2. //
  3. //  Copyright © 1994 by Rainbow Hill Pty Ltd. All rights reserved.
  4. //
  5.  
  6. #include <Errors.h>
  7. #include <Serial.h>
  8. #include <Files.h>
  9. #include <stdio.h>
  10. #include <console.h>
  11. #include <string.h>
  12.  
  13. extern pascal OSErr RAMSDOpen(SPortSel whichPort); 
  14. extern pascal void RAMSDClose(SPortSel whichPort); 
  15.  
  16. static OSErr    SetOn(void);
  17.  
  18. static ioParam    outParam;
  19. static char        outBuff[256];
  20. static SPortSel    portID;
  21. static short    refNum;
  22.  
  23. main() {
  24.     unsigned char    title[] = "SendToPort";
  25.     OSErr            osErr;
  26.     short            kIn;
  27.     short            kOut;
  28.     char            c;
  29.  
  30.     // set up the console window
  31.     console_options.procID = rDocProc;
  32.     console_options.nrows = 8;
  33.     console_options.ncols = 70;
  34.     CtoPstr(title);
  35.     console_options.title = title;
  36.  
  37.     printf("SendToPort reads strings from the keyboard and sends them to a\n");
  38.     printf("serial port. Note that SendToPort does NOT send a CR at the end\n");
  39.     printf("of each line. To send a control character, prepend a circumflex\n");
  40.     printf("accent: ^C for CNTL-C, ^I for TAB, ^M for CR, ^Z for EOF (a ^\n");
  41.     printf("\"folds\" all ASCII characters onto the first 31). Type ^^ to send\n");
  42.     printf("the circumflex accent itself.\n");
  43.     printf("\n");
  44.  
  45.     // get the port ID
  46.     do {
  47.         printf("Please identify the port (a, A, b, or B) >");
  48.         gets(outBuff);
  49.         if (*outBuff == 'a'  ||  *outBuff == 'A') {
  50.             portID = sPortA;
  51.             refNum = aoutRefNum;
  52.             }
  53.         else if (*outBuff == 'b'  ||  *outBuff == 'B') {
  54.             portID = sPortB;
  55.             refNum = boutRefNum;
  56.             }
  57.         else {
  58.             printf("\nSorry, only 'A' and 'B' are valid port identifiers\n");
  59.             *outBuff = '\0';
  60.             }
  61.         } while (*outBuff == '\0');
  62.  
  63.     // keep asking for new strings unless there is an error
  64.     printf(">");
  65.     do {
  66.         (void)gets(outBuff);
  67.  
  68.         // ignore empty strings
  69.         if (*outBuff != '\0') {
  70.  
  71.             // convert the control characters with a "look backward" algorithm
  72.             kOut = 1;
  73.             kIn = 1;
  74.             while (outBuff[kIn] != '\0') {
  75.                 c = outBuff[kIn];
  76.                 if (outBuff[kIn - 1] == '^') {
  77.                     kOut--;
  78.                     if (c != '^') {        // with ^^, the second '^' overwrites the first one
  79.                         if (c == '!')
  80.                             c = 0x7F;        // DEL
  81.                         else
  82.                             c &= 0x1F;        // CNTL character
  83.                         }
  84.                     }
  85.                 outBuff[kOut] = c;    // possible because kOut never exceeds kIn
  86.                 kIn++;
  87.                 kOut++;
  88.                 }
  89.             outBuff[kOut] = '\0';
  90.  
  91.             // set up the port
  92.             osErr = SetOn();
  93.             if (osErr == noErr) {
  94.  
  95.                 // send the string to the port
  96.                 outParam.ioReqCount = strlen((const char *)outBuff);
  97.                 osErr = PBWrite(&outParam, true);
  98.                 if (osErr != noErr) printf("PBWrite error: %d\n", osErr);
  99.  
  100.                 // close the port without KillIO(refNum) so that the string goes out
  101.                 RAMSDClose(portID);
  102.                 }
  103.  
  104.             // prompt for a new string
  105.             printf(">");
  106.  
  107.             } // the string was not empty
  108.         } while (osErr == noErr);
  109.     } // main
  110.  
  111. //------------------------------------------------------------------------------------------  SetOn
  112. static OSErr SetOn(void) {
  113.     OSErr        osErr;
  114.     SerShk        handshake;
  115.  
  116.     // set up the parameter block for transmission
  117.     outParam.ioCompletion = nil;
  118.     outParam.ioBuffer = (Ptr)outBuff;
  119.     outParam.ioPosMode = fsAtMark;
  120.     outParam.ioRefNum = refNum;
  121.  
  122.     // open the modem port for I/O
  123.     osErr = RAMSDOpen(portID);
  124.     if (osErr != noErr) {
  125.         printf("RAMSDOpen error: %d\n", osErr);
  126.         }
  127.     else {
  128.  
  129.         // configure the port
  130.         handshake.fCTS = 1;
  131.         osErr = SerReset(refNum, baud9600 | noParity | data8 | stop10);
  132.         if (osErr != noErr) {
  133.             RAMSDClose(portID);
  134.             printf("SerReset error: %d\n", osErr);
  135.             }
  136.         else {
  137.             osErr = SerHShake(refNum, &handshake);
  138.             if (osErr != noErr) {
  139.                 RAMSDClose(portID);
  140.                 printf("SerHShake error: %d\n", osErr);
  141.                 }
  142.             }
  143.         }
  144.  
  145.     return (osErr);
  146.     } // SetOn
  147.